package n1170;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;

public class Main {

	
	static int dp[][][][][]=new int[6][6][6][6][6];
	
	static{
		for(int a1=0;a1<6;a1++)
			for(int a2=0;a2<6;a2++)
				for(int a3=0;a3<6;a3++)
					for(int a4=0;a4<6;a4++)
						for(int a5=0;a5<6;a5++)
							dp[a1][a2][a3][a4][a5]=-1;
		dp[0][0][0][0][0]=0;
	}
	
	static int bac[][]=new int[5][2];
	static int offers[][]=new int[100][6];
	static int offnum;
	
	static int flag[]=new int[1000];
	
	static int solve(int a1,int a2,int a3,int a4,int a5)
	{
		if(a1<0||a2<0||a3<0||a4<0||a5<0)return -1;
		if(dp[a1][a2][a3][a4][a5]!=-1)return dp[a1][a2][a3][a4][a5];
		int tmp=a1*bac[0][1]+a2*bac[1][1]+a3*bac[2][1]+a4*bac[3][1]+a5*bac[4][1];
		for(int i=0;i<offnum;i++)
		{
			if(a1>=offers[i][0]&&a2>=offers[i][1]&&a3>=offers[i][2]&&a4>=offers[i][3]&&a5>=offers[i][4])
			{
				tmp=Math.min(tmp,solve(a1-offers[i][0],a2-offers[i][1],a3-offers[i][2],a4-offers[i][3],a5-offers[i][4])+offers[i][5]);
			}
		}
	    dp[a1][a2][a3][a4][a5]=tmp;
	    return tmp;
	}
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {

		BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
		Scanner cin=new Scanner(bf);
		int n=cin.nextInt();
		for(int i=0;i<n;i++)
		{
			int c=cin.nextInt();
			int d=cin.nextInt();
			int p=cin.nextInt();
			flag[c]=i;
			bac[i][0]=d;
			bac[i][1]=p;
		}
		offnum=cin.nextInt();
		for(int i=0;i<offnum;i++)
		{
			int k=cin.nextInt();
			for(int j=0;j<k;j++)
			{
				int c=cin.nextInt();
				int d=cin.nextInt();
				offers[i][flag[c]]=d;
			}
			offers[i][5]=cin.nextInt();
		}
		System.out.println(solve(bac[0][0],bac[1][0],bac[2][0],bac[3][0],bac[4][0]));
	}

}
